﻿using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core;
using Telerik.Windows.Controls.GridView;
using System.Collections.Generic;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class LimitsForDepartment: EFViewCmes
    {

        public LimitsForDepartment(string _dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(_dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            _view.Args.Add("fdDepotId", this.fdDepotId.GetDataValue().ToString());
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 给查看权限默认赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnAddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            UIBaseEntity temp = e.NewObject as UIBaseEntity;
            EIRow _row = temp.GetEIRow();

            if (null != _row)
            {
                _row["fdViewPermission"] = 1;
                this.grid.Columns[this.grid.Columns.Count - 1].IsReadOnly = true;
            }

        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();
            switch (type)
            {
                case "insert":
                     base.Mode = OperationMode.Insert;
                     break;
                case "update":
                     base.Mode = OperationMode.Update;
                     break;
                default:
                     break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            if (null == this.grid.SelectedItem)
            {
                base.OpenEFWindowAlert("请添加一条记录进行操作！", true);
            }

            else
            {
                EIInfo eiInfo = new EIInfo();
                EIBlock eiBlock = this.grid.EIBlock == null ? new EIBlock("detailsResult") : this.grid.EIBlock;
                eiInfo.AddBlock(eiBlock);
                List<string> _list = new List<string>();
                for(int i = 0;i < eiBlock.GetRowCount(); i++)
                {
                    string fdOrgId = eiBlock.GetRow(i)["fdOrgId"] == null ? "" : eiBlock.GetRow(i)["fdOrgId"].ToString();

                    if (fdOrgId.Equals(""))
                    {
                        base.OpenEFWindowAlert("请选择要新增的部门信息！", true);
                        return;
                    }
                    else
                    {
                        if (_list.Contains(fdOrgId))
                        {
                            base.OpenEFWindowAlert("选择的新增部门有重复！", true);
                            return;
                        }
                        else
                        {
                            _list.Add(fdOrgId);
                        }
                    }
                }
                eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCM11");
                eiInfo.Attributes.Add(EIConstants.MethodNameKey, "insert");
                eiInfo.Attributes.Add("fdDepotId", this.fdDepotId.Value);
                    
                base.Submit(eiInfo, DoInsertOperationCompleted);
            }
        }

        /// <summary>
        /// 新增操作成功后回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoInsertOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eIInfo = e.EIInfo;
            if (eIInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                base.OnCommitMessagePopup(eIInfo);//操作失败，消息提醒
            }
            else
            {
                //操作成功关闭新增页面
                //此处可以扩展是否连续新增
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void grid_CellEditEnded(object sender, Telerik.Windows.Controls.GridViewCellEditEndedEventArgs e)
        {
            
        }
    }
}
